//通过base64引入
// import http from "http"
const http = require("http");
const fs = require("fs");

const path = require("path");
//base64适合小的图标
http.createServer((req, res) => {
    let filePath = path.join(__dirname, "瓜子.png");
    let fileContent = fs.readFileSync(filePath);//得到的是图片的buffer数据
    fileContent = fileContent.toString("base64");//得到图片的base64编码数据
    let imgSrc = 'data:image/png;base64,' + fileContent;
    let imgStr = "<img src='" + imgSrc + "'/>"
    console.log(filePath);
    let htmlStr = "<!DOCTYPE html>" +
        "<html>" +
        "<head></head>" +
        "<body>" +
        "<h1>这是一个网页</h1>" + imgStr +
        "</body>" +
        "</html>";
    //响应头   200是HTTP状态码
    res.writeHead(200, { "Content-Type": "text/html;charset=utf8" });
    res.end(htmlStr);
}).listen(8081);
console.log("server is listening 8081")

